<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Antlr-Javascript:Examples/Java</title>

<!-- ANTLR includes -->
<script type="text/javascript" src="antlr3-all.js"></script>
<script type="text/javascript" src="JavaLexer.js"></script>
<script type="text/javascript" src="JavaParser.js"></script>
<script type="text/javascript" src="JavaTreeProcesser.js"></script>

<!-- Test Code -->
<script type="text/javascript">
	var sourceText = "paste Java source here";

	JavaLexer.prototype.emitErrorMessage = function(msg) {print(msg);}
	JavaParser.prototype.emitErrorMessage = function(msg) {print(msg);}
	function tick(){return +new Date;}
	function parse(text) {
	
		var theText = text;
	
		window.setTimeout(function(){
			var start = tick();
			theText = theText.replace(/\r\n?/g,"\n");
			var input = new org.antlr.runtime.ANTLRStringStream(theText);
			var lexer = new JavaLexer(input);
			var tokens = new org.antlr.runtime.CommonTokenStream(lexer);
			var parser = new JavaParser(tokens);
			var result = parser.compilationUnit();
			
			//JavaTreeProcesser.processTree(result.getTree());
			
			alert(tick()-start);
		},0);
	}
	
	var print = (function () {
		var outputBox;
		return function(text) {
			text = text.replace(/[^\r]\n/g, "\r\n");
			if(!outputBox) outputBox = document.getElementById("outputBox");
			if(typeof text == "string") {
				outputBox.appendChild(document.createTextNode(text + "\r\n"));
			} else {
				for(var i=0;i<text.length;i++) {
					outputBox.appendChild(document.createTextNode(text[i] + "\r\n"));
				}
			}
		};
	})();
	
	function processTree(tree){
		
	}
	
	function onLoad() {
		if(!this.inputElement) this.inputElement = document.getElementById("inputBox");
		inputElement.value = sourceText;
		inputElement.select();
	}
</script>
<style type="text/css">
	#outputBox {
		font-family:monospace;
		font-size:12px;
		white-space: pre;
		overflow:scroll;
		border: thin solid lightGrey;
		height: 300px;
		width:750px;
	}
</style>
</head>
<body onload="onLoad();">
    <h1>Java</h1>
    <div style="float:left">
    	<p style="font-size:8pt;">(All CR/LF's will be converted to &quot;\n&quot;)</p>
		<textarea id="inputBox" cols="90" rows="10"></textarea><br/>
		<input type="button" onclick="parse(inputElement.value);" value="Parse"/><br/>
	    <p>Output/Error:</p>
	    <div id="outputBox"></div>
    </div>
    <div style="float:right">
    </div>
</body>
</html>
